package com.cssy.xxkj.logic.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.byd.acegiext.domain.User;
import com.cssy.xxkj.logic.contants.BillStatusUtil;
import com.cssy.xxkj.logic.contants.BillTypeConstant;
import com.cssy.xxkj.logic.dao.LogicAllocatOutwhDAO;
import com.cssy.xxkj.logic.dao.LogicAllocatOutwhItemDAO;
import com.cssy.xxkj.logic.entity.LogicAllocatOutwhDO;
import com.cssy.xxkj.logic.entity.LogicAllocatOutwhItemDO;
import com.cssy.xxkj.logic.service.ILogicAllocatOutwhService;
import com.cssy.xxkj.logic.service.ILogicHsUtilService;
import com.plat.exception.SyBaseException;
import com.plat.helper.SearchConditions;
import com.plat.helper.SearchResult;
import com.plat.util.DateUtils;
import com.plat.util.GlobalResLoader;
import com.plat.util.IDCodeTypeUtil;
import com.plat.util.SessionUtil;
import com.yuendong.sy.po.code.SyCode;
import com.yuendong.sy.service.code.impl.SyCodeService;

@Service("logicAllocatOutwhService")
public class LogicAllocatOutwhServiceImpl implements ILogicAllocatOutwhService {
	
	@Resource(name = "logicAllocatOutwhDAO")
	private LogicAllocatOutwhDAO logicAllocatOutwhDAO;
	
	@Resource(name = "logicAllocatOutwhItemDAO")
	private LogicAllocatOutwhItemDAO logicAllocatOutwhItemDAO;
	
	@Resource(name = "logicHsUtilService")
	private ILogicHsUtilService logicHsUtilService;

	@Resource(name = "syCodeService")
	private SyCodeService syCodeService;
	
	@Override
	public SearchResult findListByExample(SearchConditions scs)
			throws SyBaseException {
		SearchResult rs = new SearchResult(scs);
		List<LogicAllocatOutwhDO> list = logicAllocatOutwhDAO.find(scs);
		rs.setRecordCount(logicAllocatOutwhDAO.count(scs));
		rs.setResults(list);
		return rs;
	}

	@Override
	public Integer delete(String id, User user) throws SyBaseException {
		return logicAllocatOutwhDAO.deleteLogicAllocatOutwhDOByPrimaryKey(id);
	}

	@Override
	public Integer update(LogicAllocatOutwhDO actOutwh, User user)
			throws SyBaseException {
		actOutwh.setBizModifyName(user.getName());
		actOutwh.setBizModifyDate(new Date());
		actOutwh.setBizVersionMark(actOutwh.getBizVersion());
		actOutwh.setBizVersion(actOutwh.getBizVersion()+Integer.valueOf(1));
		return logicAllocatOutwhDAO.updateLogicAllocatOutwhDO(actOutwh);
	}

	@Override
	public LogicAllocatOutwhDO save(LogicAllocatOutwhDO actOutwh, User user)
			throws SyBaseException {
		actOutwh.setBizVersion(Integer.valueOf(1));
		actOutwh.setBizCreatorName(user.getName());
		actOutwh.setBizCreatorDate(new Date());
		logicAllocatOutwhDAO.insertLogicAllocatOutwhDO(actOutwh);
		return actOutwh;
	}
	
	@Override
	public void allocatOutwhSave(HttpServletRequest request, LogicAllocatOutwhDO curAP)
			throws SyBaseException{
		String flagMark = request.getParameter("flagMark");
		String zd005MoveDate = request.getParameter("zd005MoveDate");
		String dataItem = request.getParameter("dataItem");
		String datadele = request.getParameter("datadele");
		try{
			curAP.setZd005MoveDate(DateUtils.stringToDate(zd005MoveDate));
			if(BillStatusUtil.OUTWH_BILL_STATUS_FIVE.equals(flagMark)){
				//驳回
				LogicAllocatOutwhDO outwh = logicAllocatOutwhDAO.findLogicAllocatOutwhDOByPrimaryKey(curAP.getId());
				if(outwh==null){
					throw new SyBaseException("没有记录，请查询后再操作！");
				}
				outwh.setZd002Status(flagMark);
				this.update(outwh, SessionUtil.getUser(request));
			}else{
				curAP.setZd002Status(flagMark);
				String allocatOutwhId = "";
				if(curAP==null || StringUtils.isBlank(curAP.getId())){
					//新增
					allocatOutwhId = IDCodeTypeUtil.getId();
					curAP.setId(allocatOutwhId);
					curAP.setZd001Code(syCodeService.getCode(BillTypeConstant.ALLOCATOUTWH,BillTypeConstant.ALLOCATOUTWH,SyCode.DATE_YYMM,5));
					this.save(curAP, SessionUtil.getUser(request));
				}else{
					//修改
					allocatOutwhId = curAP.getId();
					Integer row = this.update(curAP, SessionUtil.getUser(request));
					if(Integer.valueOf(1).equals(row)){
						if(StringUtils.isNotBlank(datadele)){
							List<LogicAllocatOutwhItemDO> cowhItemList = JSONArray.parseArray(datadele, LogicAllocatOutwhItemDO.class);
							for(LogicAllocatOutwhItemDO cowhItem : cowhItemList){
								if(cowhItem==null || StringUtils.isBlank(cowhItem.getId())){
									continue;
								}
								logicAllocatOutwhItemDAO.deleteLogicAllocatOutwhItemDOByPrimaryKey(cowhItem.getId());
							}
						}
					}else{
						throw new SyBaseException("更新数据错误，请重新操作！");
					}
				}
				//增加、修改行记录
				List<LogicAllocatOutwhItemDO> aotwhItemList = JSONArray.parseArray(dataItem, LogicAllocatOutwhItemDO.class);
				processAllocatOutwhItem(aotwhItemList, flagMark, allocatOutwhId, SessionUtil.getUser(request));
			}
		}catch(Exception e){
			e.printStackTrace();
			throw new SyBaseException(GlobalResLoader.getOpfailed()+ e.getMessage());
		}
	}
	
	 
	
	/**
	 * 处理调拨发起的行记录信息
	 * @param catOutwhItemList
	 * @param flagMark
	 * @param allocatOutwhId
	 */
	private void processAllocatOutwhItem(List<LogicAllocatOutwhItemDO> catOutwhItemList, String flagMark, String allocatOutwhId, User user)
			throws SyBaseException{
		try{
			if(BillStatusUtil.OUTWH_BILL_STATUS_FOUR.equals(flagMark)){
				LogicAllocatOutwhDO mainEntity = logicAllocatOutwhDAO.findLogicAllocatOutwhDOByPrimaryKey(allocatOutwhId);
				//三审完成需要修改物料的仓库库存
				
				for(LogicAllocatOutwhItemDO aotwhItem : catOutwhItemList){
					if(aotwhItem==null || StringUtils.isBlank(aotwhItem.getId()))
						continue;
					logicAllocatOutwhItemDAO.updateLogicAllocatOutwhItemDO(aotwhItem);
					//修改库存
					logicHsUtilService.processHsAllocatOutwhItem(mainEntity, aotwhItem, user);
				}
			}else{
				//不修改库存
				for(LogicAllocatOutwhItemDO aotwhItem : catOutwhItemList){
					if(aotwhItem==null)
						continue;
					if(StringUtils.isBlank(aotwhItem.getId())){
						//新增
						aotwhItem.setId(IDCodeTypeUtil.getId());
						aotwhItem.setZd001AllocatId(allocatOutwhId);
						logicAllocatOutwhItemDAO.insertLogicAllocatOutwhItemDO(aotwhItem);
					}else{
						//修改
						logicAllocatOutwhItemDAO.updateLogicAllocatOutwhItemDO(aotwhItem);
					}
				}
			}
		}catch(Exception e){
			e.printStackTrace();
			throw new SyBaseException(GlobalResLoader.getOpfailed()+ e.getMessage());
		}
	}

	@Override
	public LogicAllocatOutwhDO findListById(String id) throws SyBaseException {
		return logicAllocatOutwhDAO.findLogicAllocatOutwhDOByPrimaryKey(id);
	}

	@Override
	public Integer deleteBizRemoved(String id) throws SyBaseException {
		return logicAllocatOutwhDAO.deleteBizRemoved(id);
	}

}
